package com.qsxc.utils.validator;

import com.qsxc.business.bean.R;
import com.qsxc.system.exception.CustomException;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Objects;

/**
 * 数据校验
 */
public abstract class Assert {

    public static void isBlank(String str, String message) {
        isBlank(str, message, null);
    }

    public static void isBlank(String str, String message, Integer code) {
        if (StringUtils.isBlank(str)) {
            if (Objects.nonNull(code)) {
                throw new CustomException(message, code);
            } else {
                throw new CustomException(message);
            }
        }
    }

    public static void isNull(Object object, String message) {
        isNull(object, message, null);
    }

    public static void isNull(Object object, String message, Integer code) {
        if (Objects.isNull(object)) {
            if (Objects.nonNull(code)) {
                throw new CustomException(message, code);
            } else {
                throw new CustomException(message);
            }
        }
    }

    public static void listIsEmpty(List list, String message) {
        listIsEmpty(list, message, null);
    }

    public static void listIsEmpty(List list, String message, Integer code) {
        if (list == null || list.size() == 0) {
            if (Objects.nonNull(code)) {
                throw new CustomException(message, code);
            } else {
                throw new CustomException(message);
            }
        }
    }

    /**
     * true  给出提示
     *
     * @param b
     * @param message
     */
    public static void isNotEquals(boolean b, String message) {
        isNotEquals(b, message, null);
    }

    /**
     * true  给出提示
     *
     * @param b
     * @param resultCode
     */
    public static void isNotEquals(boolean b, R resultCode) {
        isNotEquals(b, resultCode.getMsg(), resultCode.getCode());
    }

    public static void isNotEquals(boolean b, String message, Integer code) {
        if (b) {
            if (Objects.nonNull(code)) {
                throw new CustomException(message, code);
            } else {
                throw new CustomException(message);
            }
        }
    }
}
